
package eSchool.DataLayer;

import eSchool.BL.CriteriosPesquisa;
import eSchool.BL.Disciplina;
import eSchool.BL.Disciplinas;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AccessDisciplinaDAO implements IDisciplinaDAO {
 private Connection connection = null;

    public AccessDisciplinaDAO() {
    }

    public int insertDisciplina(Disciplina disciplina) throws Exception {
        Connection con = connection;
        int resultado = -1;

        if (con == null) {
            con = AccessDAOFactory.createConnection();
        }

        String sqlIns = "Insert into Disciplina ( idDisciplina, nomeDisciplina, idDocFunc, idDocFuncResponsavel)";
        sqlIns += " values (?,?,?,?)";
        PreparedStatement pst = con.prepareStatement(sqlIns);
        pst.setString(1, disciplina.getCodDisciplina());
        pst.setString(2, disciplina.getNomeDisciplina());
        pst.setInt(3, disciplina.getDocente());
        pst.setInt(4, disciplina.getDocenteResponsavel());
        
        resultado = pst.executeUpdate();

        if (connection == null) {
            con.close();
            con = null;
        }

        return resultado;
    }

    public boolean deleteDisciplina(Disciplina disciplina) throws Exception {
        // Implement delete customer here
        // Return true on success, false on failure
        return true;
    }

    public boolean updateDisciplina(Disciplina disciplina) throws Exception {
        Connection con = connection;

        if (con == null) {
            con = AccessDAOFactory.createConnection();
        }

        String sqlUpd = " Update Disciplina SET  " +
                " nomeDisciplina = ?, " +
                " idDocFunc = ? " +
                " idDocFuncResponsavel = ? " +
                " WHERE idDisciplina = ?";

        PreparedStatement pst = con.prepareStatement(sqlUpd);
        pst.setString(1, disciplina.getNomeDisciplina());
        pst.setInt(2, disciplina.getDocente());     
        pst.setInt(3, disciplina.getDocenteResponsavel());
           
        pst.setString(4, disciplina.getCodDisciplina());

        pst.executeUpdate();

        if (connection == null) {
            con.close();
            con = null;
        }

        return true;
    }

    public Disciplinas selectDisciplinaTO(CriteriosPesquisa criterio) throws Exception {

        Disciplinas disciplinas = new Disciplinas();

        return selectDisciplinaTO(disciplinas, criterio);
    }
    
       public Disciplinas selectDisciplinaTO(Disciplinas disciplinas, CriteriosPesquisa criterio) throws Exception {
        Connection con = connection;
        Disciplina disciplina = null;
     
        if (con == null) {
            con = AccessDAOFactory.createConnection();
        }

        String sqlSlct = " SELECT idDisciplina, nomeDisciplina, idDocFunc, idDocFuncResponsavel " +
                         " FROM Disciplina";
                                         
        

        PreparedStatement pst = con.prepareStatement(sqlSlct);
        
        ResultSet rs = pst.executeQuery();
        
        while (rs.next()) {
            disciplina = new Disciplina();
            disciplina.setCodDisciplina(rs.getString("idDisciplina"));
            disciplina.setNomeDisciplina(rs.getString("nomeDisciplina"));
            disciplina.setDocente(rs.getInt("idDocFunc"));
            disciplina.setDocenteResponsavel(rs.getInt("idDocFuncResponsavel"));
            disciplinas.add(disciplina);
        }
        
        rs.close();
                
        if (connection == null) {
            con.close();
            con = null;
        }
        
        return disciplinas;
    }

    public Disciplina findDisciplina(String codDisciplina) throws Exception {
     
        Disciplina disciplina = null;
        
        return findDisciplina(disciplina, codDisciplina);
    }
    
    public Disciplina findDisciplina(Disciplina disciplina, String codDisciplina) throws Exception {
        Connection con = connection;
     
        if (con == null) {
            con = AccessDAOFactory.createConnection();
        }

        String sqlSlct = "Select idDisciplina, nomeDisciplina, idDocFunc, idDocFuncResponsavel " +
                " From Disciplina" +
                " WHERE idDisciplina = ?";

        PreparedStatement pst = con.prepareStatement(sqlSlct);
        pst.setString(1,codDisciplina);

        ResultSet rs = pst.executeQuery();
        
        while (rs.next()) {
            disciplina.setCodDisciplina(rs.getString("idDisciplina"));
            disciplina.setNomeDisciplina(rs.getString("nomeDisciplina"));
            disciplina.setDocente(rs.getInt("idDocFunc"));
            disciplina.setDocenteResponsavel(rs.getInt("idDocFuncResponsavel"));

        }
        
        rs.close();
                
        if (connection == null) {
            con.close();
            con = null;
        }
       
        return disciplina;
    }

}

